﻿<%@ Page Language="C#" AutoEventWireup="false" CodeFile="recExpenseform.aspx.cs" 
Inherits="SIPO.Web.Forms.Transaction.recExpenseform" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
     <title>Expense Form</title>
   <link href="<%= ResolveUrl("~/styles/chosen/chosen.min.css") %>" rel="stylesheet" type="text/css" />

    <script src="<%= ResolveUrl("~/scripts/plupload/moxie.min.js") %>" type="text/javascript"></script>
    <script src="<%= ResolveUrl("~/scripts/plupload/plupload.full.min.js") %>" type="text/javascript"></script>
    
    <script type="text/javascript" src="<%= ResolveUrl("~/scripts/i18n/grid.locale-id.js") %>"></script>
    <script type="text/javascript" src="<%= ResolveUrl("~/scripts/jquery.jqGrid.min.js") %>"></script>

    <script src="<%= ResolveUrl("~/scripts/jquery.validate.min.js") %>" type="text/javascript"></script>
    <script src="<%= ResolveUrl("~/scripts/jquery.validate.wrapper.js") %>" type="text/javascript"></script>

    <script src="<%= ResolveUrl("~/scripts/jquery.inputmask.bundle.min.js") %>" type="text/javascript"></script>
     <script src="<%= ResolveUrl("~/scripts/chosen/chosen.jquery.min.js") %>" type="text/javascript"></script>

    <style type="text/css">

        .style3
        {
            width: 163px;
        }
        .style4
        {
            width: 251px;
        }
    </style>
     
     <script type="text/javascript">
         $('#btnClose').click(function () {
             parent.$('#dialog1').dialog('close');
         });

    </script>
     <script type="text/javascript">

         $(document).ready(function () {

           
             $("#btnNext").button({ icons: { primary: "ui-icon-disk"} });
             $("#btnClose").button({ icons: { primary: "ui-icon-close"} });
         

         });
 
    </script>
    <script type="text/javascript">

        $(document).ready(function () {

            var uploader = new plupload.Uploader({
                runtimes: 'html5,flash,silverlight',
                browse_button: 'pickfiles',
                container: 'container',
                max_file_size: '5mb',
                url: '<%= ResolveUrl("~/AjaxHandler/Transaction/recExpenseHandler.ashx") %>' + '?method=insert',
                flash_swf_url: '<%= ResolveUrl("~/scripts/plupload/Moxie.swf") %>',
                silverlight_xap_url: '<%= ResolveUrl("~/scripts/plupload/Moxie.xap") %>',
                filters: [
            { title: "Pdf files", extensions: "pdf" },
            { title: "Excel files", extensions: "xls,xlsx" },
			{ title: "Text files", extensions: "txt" },
			{ title: "Image files", extensions: "jpeg,jpg,gif,png" },
            { title: "Word files", extensions: "doc,docx" },
			{ title: "PowerPoint files", extensions: "ppt,pptx" }
		],
                resize: { width: 320, height: 240, quality: 90 }
            });

            uploader.bind('Init', function (up, params) {

                var msg = 'Untuk dapat melakukan proses unggah / upload dokumen, browser anda harus mendukung salah satu teknologi, HTML 5 / Flash / Silverlight';

                if (params.runtime == 'html5') {
                    msg = '';
                } else if (params.runtime == 'flash') {
                    msg = '';
                } else if (params.runtime == 'silverlight') {
                    msg = ''
                }

                if (msg != '') {
                    alert(msg);
                }

            });

            uploader.init();

            uploader.bind('FilesAdded', function (up, files) {

                if (up.files.length > 1) {
                    alert('Hanya diperbolehkan satu file untuk di upload!');
                    return false;
                }

                $.each(files, function (i, file) {
                    $('#filelist').append(
                        '<div id="' + file.id + '">' +
                file.name + ' (' + plupload.formatSize(file.size) + ') <b></b>' + '</div>');
                });

                up.refresh(); // Reposition Flash/Silverlight
            });

            uploader.bind('FileUploaded', function (up, file, info) {

                var err = JSON.parse(info.response);
                alert(err.Message);

            });

            uploader.bind('Error', function (up, err) {

                var errMsg = '';

                switch (err.code) {
                    case (-600):
                        errMsg = 'File tidak boleh lebih dari 5 Mb';
                        break;
                    case (-601):
                        errMsg = 'File harus berformat excel / word/ text / pdf / gambar';
                        break;
                    default:
                        errMsg = err.code + ' - ' + err.message;
                        break;
                }

                alert(errMsg);

                up.refresh(); // Reposition Flash/Silverlight

            });


            var AwalInvoice = $('#txtInvoiceNo').val();
            $('#ddlAlocation').change(function () {
                var temp = $('#ddlAlocation option:selected').text();
                var temp1 = [];
                temp1.push(temp.split('-', 2));
                $('#hfAlocationName').val(temp1[0]);
            });
            
            var review = '<%= Request.Params["review"] %>';

            $('#trReview').hide();
            $('#trtxtAlokasi').hide();
            $('#trtxtAlokasi').val($('#ddlAlocation').text());
            var rules;
            var messages;
            if (review != "") {
                $('#trReview').show();
                $('#trtxtAlokasi').show();
                $('#trAttach').hide();
                $('#trddlAlokasi').hide();
                $('#txtTransDate').attr("disabled", "disabled");
                $('#txtValue').attr("disabled", "disabled");
                $('#txtTujuan').attr("disabled", "disabled");
                $('#ddlAlocation').attr("disabled", "disabled");
                $('#txtInvoiceNo').attr("disabled", "disabled");
                $('#txtAlokasi').attr("disabled", "disabled");

                rules = {
                    txtReviewDate: "required"
                };
                messages = {
                    txtReviewDate: "Tanggal persetujuan harus di isi"
                };
            }
            else {
                rules = {
                  //  ddlAlocation: { dropDownValidator: true },
                    TransDate: "required",
                    txtInvoiceNo: "required",
                    txtValue: "required",
                    txtTujuan: "required"

                };
                messages = {
                   // ddlAlocation: "Alokasi harus dipilih",
                    txtInvoiceNo: "No. bukti harus di isi",
                    txtValue: "Nilai harus diisi",
                    txtTujuan: "Tujuan Pengeluaran harus di isi",
                    TransDate: "Tanggal harus di isi"
                };
            }
            var date = new Date();
            var currentMonth = date.getMonth();
            var currentDate = date.getDate();
            var currentYear = date.getFullYear();

            if (review == "") {
                $("#txtTransDate").datepicker({ showWeek: true,
                    showButtonPanel: true,
                    changeMonth: true,
                    changeYear: true,
                    dateFormat: 'dd M yy',
                    maxDate: new Date (currentYear, currentMonth, currentDate),
                    onClose: function () {
                        $('#txtInvoiceNo').focus();
                    }
                });
                $("#txtTransDate").focus();
                $("#txtTransDate").datepicker("hide");
            }
            else {
                $("#txtReviewDate").datepicker({ showWeek: true,
                    showButtonPanel: true,
                    changeMonth: true,
                    changeYear: true,
                    dateFormat: 'dd M yy',
                    minDate: new Date($("#txtTransDate").val()),
                    onClose: function () {
                        $('#btnNext').focus();
                    }
                });
                $('#txtReviewDate').focus();
                $("#txtReviewDate").datepicker("hide");
            }

            $.validator.addMethod("dropDownValidator", function (value, element, parameters) {
                return (value != '');
            });
            //            var rules = {
            //                ddlAlocation: { dropDownValidator: true },
            //                TransDate: "required",
            //                txtInvoiceNo: "required",
            //                txtValue: "required",
            //                txtTujuan: "required"
            //                //txtDebtorValue: "number",
            //                //txtOJKValue: "number"
            //            };
            //            var messages = {
            //                ddlAlocation: "Alokasi harus dipilih",
            //                txtInvoiceNo: "Invoice harus di isi",
            //                txtValue: "Nilai harus diisi",
            //                txtTujuan: "Tujuan Pengeluaran harus di isi"
            //                //txtDebtorValue: "Nilai pengenaan Debitur harus di isi dengan angka",
            //                //txtOJKValue: "Nilai pengenaan OJK harus di isi dengan angka"
            //            };
            var id = $('#hfID').val();
            var validator = new jQueryValidatorWrapper("form1", rules, messages);
            $('#txtValue').inputmask("decimal", { radixPoint: ",", autoGroup: true, groupSeparator: ".", groupSize: 3, digits: 2, repeat: 18 });
            $("#btnNext").click(function (e) {

                e.preventDefault();

                if (!validator.validate())
                    return;
                if ($('#txtTransDate').val() != "") {

                    var id = $('#txtInvoiceNo').val();
                    var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Loaddata/validasiExpenseCode.ashx?id=' + id + '") %>';
                    var ok = 0;
                    if (review == "" && id != AwalInvoice) {

                        jQuery.ajax({
                            type: 'POST',
                            url: handlerUrl,
                            contentType: 'application/json',
                            dataType: 'json',
                            success: function (data) {
                                if (data.Hasil > 0) {
                                    alert("No bukti sudah ada !");
                                    $('#txtInvoiceNo').val('');
                                    $('#txtInvoiceNo').focus();
                                }
                                else {
                                   

                                    if (uploader.files.length  < 1) { // Insert without uploadedfile

                                        $.post('<%= ResolveUrl("~/AjaxHandler/Transaction/recExpensehandler.ashx") %>' + '?method=insert',
                                $("#form1")
                                .find("input,textarea,select,hidden")
                                .not("#__VIEWSTATE,#__EVENTVALIDATION")
                                .serialize())
                            .done(function (data) {
                                var r = JSON.parse(data);
                                alert(r.Message);
                                //location.href = '<%= ResolveUrl("~/lists/Transaction/recImpositionAlllist.aspx") %>';
                            })
                            .fail(function () {
                                alert('Data gagal di simpan!');
                            });

                                    }
                                    else {

                                        if (uploader.files.length > 0) // Insert with uploaded file.
                                        {
                                            var TransDate = $('#txtTransDate').val();
                                            var InvoiceNo = $('#txtInvoiceNo').val();
                                            var Value = $('#txtValue').val();
                                            var AlocationID = $('#ddlAlocation').val();
                                            var Tujuan = $('#txtTujuan').val();
                                            var AlocationName = $('#hfAlocationName').val();
                                         
                                            uploader.settings.multipart_params = {
                                                'txtTransDate': encodeURIComponent(TransDate),
                                                'txtInvoiceNo': encodeURIComponent(InvoiceNo),
                                                'txtValue': encodeURIComponent(Value),
                                                'ddlAlocation': encodeURIComponent(AlocationID),
                                                'txtTujuan': encodeURIComponent(Tujuan),
                                                'hfAlocationName': encodeURIComponent(AlocationName),
                                            

                                            };
                                            uploader.settings.url = '<%= ResolveUrl("~/AjaxHandler/Transaction/recExpensehandler.ashx") %>' + '?method=insert';
                                            uploader.start();
                                        }
                                    }





                                    parent.$('#dialog1').dialog('close');

                                    setTimeout("parent.$('#table').trigger('reloadGrid');", 3000);

                                    return false;
                                }
                            },
                            error: function () {
                                alert('Error!');
                            }
                        });

                    }
                    else {


                        if (uploader.files.length < 1) { // Insert without uploadedfile

                            $.post('<%= ResolveUrl("~/AjaxHandler/Transaction/recExpensehandler.ashx") %>' + '?method=insert',
                                $("#form1")
                                .find("input,textarea,select,hidden")
                                .not("#__VIEWSTATE,#__EVENTVALIDATION")
                                .serialize())
                            .done(function (data) {
                                var r = JSON.parse(data);
                                alert(r.Message);
                                //location.href = '<%= ResolveUrl("~/lists/Transaction/recImpositionAlllist.aspx") %>';
                            })
                            .fail(function () {
                                alert('Data gagal di simpan!');
                            });

                        }
                        else {
                            if (uploader.files.length > 0) // Insert with uploaded file.
                            {
                                var TransDate = $('#txtTransDate').val();
                                var InvoiceNo = $('#txtInvoiceNo').val();
                                var Value = $('#txtValue').val();
                                var AlocationID = $('#ddlAlocation').val();
                                var Tujuan = $('#txtTujuan').val();
                                var AlocationName = $('#hfAlocationName').val();
                                var ID = $('#hfID').val();
                                uploader.settings.multipart_params = {
                                    'txtTransDate': encodeURIComponent(TransDate),
                                    'txtInvoiceNo': encodeURIComponent(InvoiceNo),
                                    'txtValue': encodeURIComponent(Value),
                                    'ddlAlocation': encodeURIComponent(AlocationID),
                                    'txtTujuan': encodeURIComponent(Tujuan),
                                    'hfAlocationName': encodeURIComponent(AlocationName),
                                    'hfID': encodeURIComponent(ID)

                                };
                                uploader.settings.url = '<%= ResolveUrl("~/AjaxHandler/Transaction/recExpensehandler.ashx") %>' + '?method=insert';
                                uploader.start();
                            }
                        }





                        parent.$('#dialog1').dialog('close');

                        setTimeout("parent.$('#table').trigger('reloadGrid');", 3000);

                        return false;
                    }

                }
                else {
                    alert("Lengkapi data yang belum di isi !")
                }
            });

        });

   </script>
</head>
<body>
    <form id="form1" runat="server">
    <table border="0">
        <tr>
            <td class="style3">
                Tgl. Transaksi
            </td>
            <td>
                :
            </td>
            <td class="style4">
                <asp:TextBox ID="txtTransDate" runat="server" Width="200" 
                    Style="text-align: right"></asp:TextBox>
                &nbsp;<span style="color: Red;">*</span>
            </td>
        </tr>
        <tr>
            <td class="style3">
                No. Bukti</td>
            <td>
                :
            </td>
            <td class="style4">
                <asp:TextBox ID="txtInvoiceNo" runat="server" Width="200" 
                    Style="text-align: left"></asp:TextBox>
                &nbsp;<span style="color: Red;">*</span>
            </td>
        </tr>
         <tr>
            <td class="style3">
               Nilai</td>
            <td>
                :
            </td>
            <td class="style4">
                <asp:TextBox ID="txtValue" runat="server" Width="200" 
                    Style="text-align: right"></asp:TextBox>
                &nbsp;<span style="color: Red;">*</span>
            </td>
        </tr>
        <tr id="trddlAlokasi">
            <td class="style3">
                Alokasi
            </td>
            <td>
                :
            </td>
            <td class="style4">
                <asp:DropDownList ID="ddlAlocation" runat="server" Width="250px" Height="22px">
                </asp:DropDownList>
            </td>
        </tr>
        <tr id="trtxtAlokasi">
            <td class="style3">
                Alokasi
            </td>
            <td>
                :
            </td>
            <td class="style4">
                <asp:TextBox ID="txtAlokasi" runat="server" Width="200px" 
                    Style="text-align: left" Height="35px" TextMode="MultiLine"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style3">
                Tujuan Pengeluaran
            </td>
            <td>
                :
            </td>
            <td class="style4">
                <asp:TextBox ID="txtTujuan" runat="server" Width="200px" 
                    Style="text-align: left" Height="60px" TextMode="MultiLine"></asp:TextBox>
            </td>
        </tr>
        <tr id="trAttach">
            <td class="style3">
                Lampiran
             </td>
            <td>
                :
            </td>
            <td class="style4">
               <div id="container">
                    <a id="pickfiles" class="plupload_button plupload_add ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-primary"
                        href="#"><span class="ui-button-icon-primary ui-icon ui-icon-circle-plus" /><span
                            class="ui-button-text">Add Files</span> </a>
                    <br />
                    <div id="filelist"></div>
                </div>
            </td>
        </tr>
          <tr id="trReview">
            <td class="style3">
                Tgl. Persetujuan</td>
            <td>
                :
            </td>
            <td class="style4">
                <asp:TextBox ID="txtReviewDate" runat="server" Width="200" 
                    Style="text-align: right"></asp:TextBox>
                &nbsp;<span style="color: Red;">*</span>
            </td>
        </tr>
    </table>
    <table border="0">
        <tr>
            <td>
                &nbsp;
            </td>
            <td>
                <div style="text-align: right; padding-top: 10px;">
                    <button id="btnNext" type="button">
                        Simpan</button>
                    <button id="btnClose" type="button">
                        Batal</button>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;
            </td>
            <td>
                <asp:HiddenField ID="hfID" runat="server" />
                <asp:HiddenField ID="hfAlocationName" runat="server" />
                <asp:HiddenField ID="hfTarifId" runat="server" />
                <asp:HiddenField ID="hfTarifValue" runat="server" />
            </td>
        </tr>
    </table>
    </form>
</body>
</html>
